<?php

namespace app\extra\H5WeixinSDK\V3;

class WxPayPubHelper {

    public function requestpayjsapi($post) {

        header("Content-type: text/html; charset=utf-8");
        //使用jsapi接口
        $jsApi = new JsApipub();

        //=========步骤1：网页授权获取用户openid============
        //通过code获得openid
        if (!isset($post['code'])) {
            //触发微信返回code码
            $url = $jsApi->createOauthUrlForCode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
            Header("Location: $url");
            exit;
        } else {
            //获取code码，以获取openid
            $code = $_GET['code'];

            $jsApi->setCode($code);
            $openid = $jsApi->getOpenId();
        }

        //=========步骤2：使用统一支付接口，获取prepay_id============
        //使用统一支付接口
        $unifiedOrder = new UnifiedOrderpub();

        //设置统一支付接口参数
        //设置必填参数
        //appid已填,商户无需重复填写
        //mch_id已填,商户无需重复填写
        //noncestr已填,商户无需重复填写
        //spbill_create_ip已填,商户无需重复填写
        //sign已填,商户无需重复填写 
        $unifiedOrder->setParameter("openid", $openid); //商品描述 

        $unifiedOrder->setParameter("body", '订单编号：' . $post['out_trade_no']); //商品描述
        //自定义订单号，此处仅作举例

        $unifiedOrder->setParameter("out_trade_no", $post['out_trade_no']); //商户订单号
        $unifiedOrder->setParameter("total_fee", $post['order_amount']); //总金额
        $unifiedOrder->setParameter("notify_url", WxPayConfpub::NOTIFY_URL); //通知地址
        $unifiedOrder->setParameter("trade_type", "JSAPI"); //交易类型 
        $prepay_id = $unifiedOrder->getPrepayId();
        //=========步骤3：使用jsapi调起支付============
        $jsApi->setPrepayId($prepay_id);
        return $jsApi->getParameters();
    }

    public function requestpayapp($post) {

        header("Content-type: text/html; charset=utf-8");
        //使用jsapi接口
        $jsApi = new JsApipub();

        //=========步骤2：使用统一支付接口，获取prepay_id============
        //使用统一支付接口
        $unifiedOrder = new UnifiedOrderpub();

        //设置统一支付接口参数
        $unifiedOrder->setParameter("body", '订单编号：' . $post['out_trade_no']); //商品描述
        //自定义订单号，此处仅作举例

        $unifiedOrder->setParameter("out_trade_no", $post['out_trade_no']); //商户订单号
        $unifiedOrder->setParameter("total_fee", $post['order_amount']); //总金额
        $unifiedOrder->setParameter("notify_url", WxPayConfpubapp::NOTIFY_URL); //通知地址
        $unifiedOrder->setParameter("trade_type", "APP"); //交易类型

        $parameter = $unifiedOrder->getPrepayIdapp();
        return $jsApi->getParametersapp($parameter);
    }

    public function requestpaywap($post) {

        header("Content-type: text/html; charset=utf-8");
        //使用jsapi接口
        $jsApi = new JsApipub();

        //=========步骤2：使用统一支付接口，获取prepay_id============
        //使用统一支付接口
        $unifiedOrder = new UnifiedOrderpub();

        //设置统一支付接口参数
        $unifiedOrder->setParameter("body", '订单编号：' . $post['out_trade_no']); //商品描述
        //自定义订单号，此处仅作举例

        $unifiedOrder->setParameter("device_info", "WEB"); //商户订单号
        $unifiedOrder->setParameter("scene_info", '{"h5_info": {"type":"Wap","wap_url": "https://hi.qiu7.net","wap_name": "充值"}} '); //商户订单号

        $unifiedOrder->setParameter("out_trade_no", $post['out_trade_no']); //商户订单号
        $unifiedOrder->setParameter("total_fee", $post['order_amount']); //总金额
        $unifiedOrder->setParameter("notify_url", WxPayConfpub::NOTIFY_URL); //通知地址
        $unifiedOrder->setParameter("trade_type", "MWEB"); //交易类型

        $parameter = $unifiedOrder->getPrepayIdapp();
        return $jsApi->getParametersapp2($parameter);
    }

    //二维码扫描
    public function qrcode($post) {

        header("Content-type: text/html; charset=utf-8");
        $unifiedOrder = new UnifiedOrderpub();
        $unifiedOrder->setParameter("body", "订单编号：" . $post['out_trade_no']); //商品描述	 	 
        $unifiedOrder->setParameter("out_trade_no", $post['out_trade_no']); //商户订单号
        $unifiedOrder->setParameter("total_fee", $post['order_amount']); //总金额
        $unifiedOrder->setParameter("notify_url", WxPayConfpub::NOTIFY_URL); //通知地址
        $unifiedOrder->setParameter("trade_type", "NATIVE"); //交易类型
        $unifiedOrder->setParameter("product_id", $post['out_trade_no']); //交易类型 
        //获取统一支付接口结果

        $unifiedOrderResult = $unifiedOrder->getResult();
        //  var_dump($unifiedOrderResult);
        // exit;
        //商户根据实际情况设置相应的处理流程
        if ($unifiedOrderResult["return_code"] == "FAIL") {
            return false;
        } elseif ($unifiedOrderResult["result_code"] == "FAIL") {
            return false;
        } elseif ($unifiedOrderResult["code_url"] != NULL) {
            //从统一支付接口获取到code_url
            $code_url = $unifiedOrderResult["code_url"];
        }
        return $code_url;
    }

    //领取红包
    public function sendredpack($sendredinfo) {
        header("Content-type: text/html; charset=utf-8");
        //使用jsapi接口
        $jsApi = new JsApipub();
        //使用统一支付接口
        $unifiedOrder = new UnifiedOrderpub();
        $mch_billno = WxPayConfpub::MCHID . date('Ymd') . $sendredinfo['mch_billno'];
        $business = \Business\ConfigModel::getInstance();
        $unifiedOrder->setParameter("mch_billno", $mch_billno); //商户订单号
        $unifiedOrder->setParameter("nick_name", $sendredinfo['nick_name']); //提供方名称
        $unifiedOrder->setParameter("send_name", $sendredinfo['send_name']); //商户名称
        $unifiedOrder->setParameter("re_openid", $sendredinfo['openid']); //微信OPENID
        $unifiedOrder->setParameter("total_amount", $sendredinfo['total_amount']); //付款金额
        $unifiedOrder->setParameter("min_value", $sendredinfo['min_value']); //最小红包金额
        $unifiedOrder->setParameter("max_value", $sendredinfo['max_value']); //最大红包金额 
        $unifiedOrder->setParameter("total_num", $sendredinfo['total_num']); //红包収放总人数 
        $unifiedOrder->setParameter("wishing", $sendredinfo['wishing']); //红包祝福
        $unifiedOrder->setParameter("act_name", $sendredinfo['act_name']); //活劢名称
        $unifiedOrder->setParameter("remark", $sendredinfo['remark']); //备注 
        $unifiedOrder->setParameter("logo_imgurl", "http://pbaimage.pba.cn/2014/new_pba/images/logo.png"); //商户订单号 
        $unifiedOrder->setParameter("share_content", $sendredinfo['remark']); //商户订单号 
        $unifiedOrder->setParameter("share_url", "http://pba.cn"); //商户订单号 
        $unifiedOrder->setParameter("share_imgurl", 'http://pbaimage.pba.cn/2014/new_pba/images/logo.png'); //商户订单号 

        $unifiedOrderResult = $unifiedOrder->postXmlSSL();
        \Our\Log::getInstance()->write('微信红包，回调数据：' . var_export($unifiedOrderResult, true), 'WXredpage.log');

        return Commonutilpub::xmlToArray($unifiedOrderResult);
    }

    /**
     * 获取微信用户信息
     */
    public function getuserinfo($post = array(), $scope = 'snsapi_base') {
        $jsApi = new JsApipub();
        header("Content-type: text/html; charset=utf-8");
        if (!isset($post['code']) || empty($post['code'])) {
            //触发微信返回code码
            $url = $jsApi->createOauthUrlForCode('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $scope);

            Header("Location: $url");
            exit;
        } else {
            //使用jsapi接口
            $jsApi = new JsApipub();
            //=========步骤1：网页授权获取用户openid============
            //通过code获得openid
            //获取code码，以获取openid     
            $jsApi->setCode($post['code']);

            $openidAll = $jsApi->getOpenidall();
            if (empty($openidAll)) {
                return array();
            }
            $openid = $openidAll['openid'];

            //微信用户信息
            //采用snsapi_base使用opeid加上普通access_token获取用户信息
            if ($scope == "snsapi_base") {
                $userinfo = $jsApi->getOpeniduserinfo($openid);
            } else {
                //采用snsapi_userinfo使用网页授权access_token获取用户信息  
                $userinfo = $jsApi->getOpeniduserinfo($openid, $openidAll['access_token']);
            }
            return $userinfo;
        }

        return array();
    }

}

?>
